import time, os
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from public_parameters import TEST_RESULT_PATH

Base = declarative_base()

class Records(Base):
    __tablename__ = 'records'

    id = Column(Integer, primary_key=True)
    case_name = Column(String)
    case_action = Column(String)
    case_desc = Column(String)
    case_expect = Column(String)
    case_result = Column(Boolean)
    case_output_status = Column(String)
    case_output_reason = Column(String)
    case_output_file = Column(Text)
    case_input_file = Column(Text)
    case_isEqual = Column(String)
    case_name2 = Column(String)
    case_ctype = Column(String)

    def __init__(self, case_name, case_action, case_desc, case_expect, case_result,
                 case_output_status, case_output_reason, case_output_file, case_input_file, case_isEqual,
                 case_name2, case_ctype):
        self.case_name = case_name
        self.case_action = case_action
        self.case_desc = case_desc
        self.case_expect = case_expect
        self.case_result = case_result
        self.case_output_status = case_output_status
        self.case_output_reason = case_output_reason
        self.case_output_file = case_output_file
        self.case_input_file = case_input_file
        self.case_isEqual = case_isEqual
        self.case_name2 = case_name2
        self.case_ctype = case_ctype

class PrinterInfo(Base):
    __tablename__ = 'info'
    id = Column(Integer, primary_key=True)
    p_name = Column(String)
    p_firmware_no = Column(String)
    p_serial_no = Column(String)
    p_connType = Column(String)
    ticket_name = Column(String)
    tree_list = Column(String)

    def __init__(self, p_name, p_firmware_no, p_serial_no, p_connType, ticket_name, tree_list):
        self.p_name = p_name
        self.p_firmware_no = p_firmware_no
        self.p_serial_no = p_serial_no
        self.p_connType = p_connType
        self.ticket_name = ticket_name
        self.tree_list = tree_list

class CheckDiscoveryTree(Base):
    __tablename__ = "discovery_tree"
    id = Column(Integer, primary_key=True)
    t_path = Column(String)
    t_action = Column(String)
    t_result = Column(String)
    t_reason = Column(String)
    t_output = Column(String)
    t_ctype = Column(String)

    def __init__(self, t_path, t_action, t_result, t_reason, t_output, t_ctype):
        self.t_path = t_path
        self.t_action = t_action
        self.t_result = t_result
        self.t_reason = t_reason
        self. t_output = t_output
        self.t_ctype = t_ctype

class CheckDiscoveryTree_2(Base):
    __tablename__ = "discovery_tree_2"
    id = Column(Integer, primary_key=True)
    eTree_local = Column(String)
    eTree_Printer = Column(String)
    isAllTagsEqual = Column(String)
    isAllTextEqual = Column(String)

    def __init__(self, eTree_local, eTree_Printer, isAllTagsEqual, isAllTextEqual):
        self.eTree_local = eTree_local
        self.eTree_Printer = eTree_Printer
        self.isAllTagsEqual = isAllTagsEqual
        self.isAllTextEqual = isAllTextEqual

class DatabaseAction(object):
    def __init__(self):
        self.result_path = TEST_RESULT_PATH
        self.dbName = '%s.sqlite' % self.current_time()
        self.__save_update_dbName(self.dbName)
        self.engine = create_engine('sqlite:///' + self.result_path + os.sep + self.dbName)
        self.Session = sessionmaker(bind=self.engine)
        self.session = self.Session()
        Base.metadata.create_all(self.engine)

    def __save_update_dbName(self, dbName):
        with open(os.path.join(TEST_RESULT_PATH, '.dbName'), 'w') as fp:
            fp.write(self.dbName)

    def current_time(self):
        a=time.localtime()
        year=a.tm_year
        month=a.tm_mon
        day=a.tm_mday
        hour=a.tm_hour
        minute=a.tm_min
        sec=a.tm_sec
        ct='%s_%s_%s_%s_%s_%s'%(year,month,day,hour,minute,sec)
        return ct

    def insert_data(self, *args):
        """"""
        tmp = Records(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8], args[9], args[10], args[11])
        self.session.add(tmp)
        self.session.commit()

    def insert_printer_info(self, *args):
        """"""
        tmp = PrinterInfo(args[0], args[1], args[2], args[3], args[4], args[5])
        self.session.add(tmp)
        self.session.commit()

    def insert_dTree_check(self, *args):
        """"""
        tmp = CheckDiscoveryTree(args[0], args[1], args[2], args[3], args[4], args[5])
        self.session.add(tmp)
        self.session.commit()

    def insert_dTree_check_2(self, *args):
        tmp = CheckDiscoveryTree_2(args[0], args[1], args[2], args[3])
        self.session.add(tmp)
        self.session.commit()

    def close_database(self):
        self.session.close()